using Services;
using System.Collections.Generic;
using UnityEngine;

[DefaultExecutionOrder(100)]
public class PlayerInput : MonoBehaviour
{
    public const float MoveInterval = 0.1f;

    private EventSystem eventSystem;
    private GameManager gameManager;
    private Dictionary<EGameState, SubController> subs;


    protected void Awake()
    {
        eventSystem = ServiceLocator.Get<EventSystem>();
        gameManager = ServiceLocator.Get<GameManager>();
        InitializeSubControllers();

        eventSystem.AddListener<EGameState, EGameState>(EEvent.GameStateChange, OnGameStateChange);
        eventSystem.AddListener<bool>(EEvent.Pause, OnPause);
    }

    private void OnPause(bool paused)
    {
        if (paused)
            BanInput();
        else
            subs[gameManager.GameState].Active = true;
    }

    private void OnGameStateChange(EGameState from, EGameState to)
    {
        BanInput();
        if (subs.ContainsKey(to))
            subs[to].Active = true;
    }

    private void InitializeSubControllers()
    {
        subs = new Dictionary<EGameState, SubController>()
        {
            {EGameState.Default,new SubController_Default() },
            {EGameState.OpenSkillPanel,new SubController_OpenSkillPanel() },
            {EGameState.ChooseTarget,new SubController_ChooseTarget() },
            {EGameState.OpenBag,new SubController_OpenBag() },
            {EGameState.OpenStorage,new SubController_OpenStorage() },
            {EGameState.OpenSkillTree,new SubController_OpenSkillTree() },
            {EGameState.Purchase,new SubController_Purchase() },
            {EGameState.OpenPauseMenu,new SubController_OpenPauseMenu() },
        };
    }

    private void BanInput()
    {
        foreach (SubController sub in subs.Values)
        {
            sub.Active = false;
        }
    }
}
